import { createFileRoute } from '@tanstack/react-router'
import Roles from '@/features/roles'
import { roleKeys, roles, rolesSelectTree, rolesTableTree } from '@/api/roles'
import { ruleKeys, rules, rulesSelectTree } from '@/api/rules'
import { Role } from '@/features/roles/data/schema'
import { ListItem, TreeItem } from '@/components/tree-select/schema'
export const Route = createFileRoute('/_authenticated/roles/')({
  component: Roles,
  loader: async ({ context: { queryClient } }): Promise<{ roleTable: Role[], roleSelect: TreeItem[], roleList: ListItem[], ruleSelect: TreeItem[], ruleList: ListItem[] }> => {
    const roleResponse = await queryClient.fetchQuery({
      queryKey: roleKeys.rolesTableTree(),
      queryFn: rolesTableTree
    })
    const roleListResponse = await queryClient.fetchQuery({
      queryKey: roleKeys.all(),
      queryFn: roles
    })
    const roleSelectResponse = await queryClient.fetchQuery({
      queryKey: roleKeys.rolesSelectTree(),
      queryFn: rolesSelectTree
    })
    const ruleResponse = await queryClient.fetchQuery({
      queryKey: ruleKeys.all(),
      queryFn: rules
    })
    const ruleSelectResponse = await queryClient.fetchQuery({
      queryKey: ruleKeys.rulesSelectTree(),
      queryFn: rulesSelectTree
    })
    return { roleTable: roleResponse.data.data, roleSelect: roleSelectResponse.data.data, roleList: roleListResponse.data.data, ruleSelect: ruleSelectResponse.data.data, ruleList: ruleResponse.data.data }
  }
})
